extends RefCounted
class_name StraightLine
"""
直线方程 a * x + b * y + c = 0
"""


var a: float
var b: float
var c: float


# 从2个坐标点生成
static func from_point(p1: Vector2, p2: Vector2) -> StraightLine:
    var _a: float = p1.y - p2.y
    var _b: float = p2.x - p1.x
    var _c: float = _a * p1.x + _b * p1.y

    var line: StraightLine = StraightLine.new()
    line.a = _a
    line.b = _b
    line.c = _c
    return line

# 从方向生成
static func from_direction(direction: Vector2) -> StraightLine:
    return from_point(Vector2.ZERO, direction)

# 根据X计算Y
func get_y(x: float) -> float:
    return -(a * x + c) / b

# 返回与另外一条直线是否相交
func is_cross(other: StraightLine) -> bool:
    var det_k: float = a * other.b - other.a * b
    return is_zero_approx(det_k)

# 计算与另外一条直线交叉的点
func get_intersect_point(line_to: StraightLine) -> Vector2:
    var det_k: float = a * line_to.b - line_to.a * b
    var _a: float = line_to.b / det_k
    var _b: float = -1 * b / det_k
    var _c: float = -1 * line_to.a / det_k
    var _d: float = a / det_k
    var x: float = _a * c + _b * line_to.c
    var y: float = _c * c + _d * line_to.c
    return Vector2(x, y)
